package cn.com.gridinfo.sooc.core.login.activity.forgetpwd;

import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.SoocApplication;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.login.adapter.NodeServerAdapter;
import cn.com.gridinfo.sooc.core.login.bean.NodeServerInfo;
import cn.com.gridinfo.sooc.core.login.common.CodeConst;
import cn.com.gridinfo.sooc.core.login.dao.ForgetDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 获取学校列表
 * By 刘晓晨
 */
public class ServiceListActivity extends BaseSoocActivity implements AdapterView.OnItemClickListener {

    @InjectView(R.id.serviceList)
    ListView serviceList;//选择账号注册下的节点
    NodeServerAdapter adapter;//适配器
    List<NodeServerInfo> list;//学校名称集合

    ForgetDao dao;//用户Dao
    private String myAcoount;//用户账号


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.service_list_activity);
        SoocApplication.getContext().addActivity(this);
        ButterKnife.inject(this);
        dao = new ForgetDao(this);
        //数据初始化
        list = new ArrayList<>();
        adapter = new NodeServerAdapter(this, list);
        serviceList.setAdapter(adapter);
        //获取数据
        myAcoount = getIntent().getStringExtra("userAccount");
        Log.d("账号：" + myAcoount);
        load(myAcoount);
        serviceList.setOnItemClickListener(this);

    }

    //请求接口
    private void load(String account) {
        dao.getMySchoolList(account);
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "选择学校";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //ToolBar的back键
    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        finish();
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        switch (requestCode) {
            case CodeConst.GET_MYSCHOOLLIST://获得注册节点列表
                Log.d("dao.getSchoolInfoList()" + dao.getSchoolInfoList().toString());
                list.addAll(dao.getSchoolInfoList());
                Log.d("list的信息：" + list.toString());
                adapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    public void onRequestFaild(int requestCode, String errorNo, String errorMessage) {
        super.onRequestFaild(requestCode, errorNo, errorMessage);
        MessageUtils.showImageShortToast(this, "服务器异常");
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //跳转到找回密码详情页面
        Intent intent = new Intent(this, BackPwdModeActivity.class);
        intent.putExtra("account", myAcoount);
        intent.putExtra("xtdz", list.get(position).getXtdz());
        dao.setXtdz(list.get(position).getXtdz());
        startActivity(intent);
        AnimUtil.intentSlidIn(this);
    }
}
